import { useEffect } from 'react';
import { useCourseStore } from './courseStore';
import { courseSelectors } from './courseSelectors';
import type { ICourse, ICourseCreateRequest, ICourseUpdateRequest } from '@/types/course/course';
import type { ICourseCategory, ICourseCategoryCreateRequest, ICourseCategoryUpdateRequest } from '@/types/course/courseCategory';

/**
 * 课程管理 Hook
 * @description 提供课程管理的完整功能，包括数据操作、UI状态管理等
 */
export const useCourseManagement = () => {
  const store = useCourseStore();

  // 初始化数据
  useEffect(() => {
    if (store.courses.length === 0 && !store.courseAsync.loading) {
      store.fetchCourses();
    }
    if (store.categories.length === 0 && !store.categoryAsync.loading) {
      store.fetchCategories();
      store.fetchCategoryTree();
    }
  }, [
    store.courses.length, 
    store.categories.length, 
    store.courseAsync.loading, 
    store.categoryAsync.loading
  ]);

  return {
    // 课程数据状态
    courses: courseSelectors.getFilteredCourses(store),
    allCourses: courseSelectors.getAllCourses(store),
    selectedCourse: store.selectedCourse,
    selectedCourses: courseSelectors.getSelectedCourses(store),
    
    // 分类数据状态
    categories: courseSelectors.getFilteredCategories(store),
    allCategories: courseSelectors.getAllCategories(store),
    selectedCategory: store.selectedCategory,
    categoryTree: courseSelectors.getCategoryTree(store),
    
    // 异步状态
    isCourseLoading: courseSelectors.isCourseLoading(store),
    isCategoryLoading: courseSelectors.isCategoryLoading(store),
    courseError: courseSelectors.getCourseError(store),
    categoryError: courseSelectors.getCategoryError(store),
    courseLastUpdated: courseSelectors.getCourseLastUpdated(store),
    categoryLastUpdated: courseSelectors.getCategoryLastUpdated(store),
    
    // UI状态
    showCourseForm: store.showCourseForm,
    courseFormMode: store.courseFormMode,
    showCourseDetail: store.showCourseDetail,
    showCourseBatchPanel: store.showCourseBatchPanel,
    showCategoryForm: store.showCategoryForm,
    categoryFormMode: store.categoryFormMode,
    showCategoryPanel: store.showCategoryPanel,
    
    // 选择状态
    selectedCourseCount: courseSelectors.getSelectedCourseCount(store),
    hasSelectedCourses: courseSelectors.hasSelectedCourses(store),
    isAllCoursesSelected: courseSelectors.isAllCoursesSelected(store),
    isCoursesIndeterminate: courseSelectors.isCoursesIndeterminate(store),
    
    // 筛选状态
    courseFilters: store.courseFilters,
    categoryFilters: store.categoryFilters,
    
    // 统计信息
    courseStatistics: courseSelectors.getCourseStatistics(store),
    categoryStatistics: courseSelectors.getCategoryStatistics(store),
    teachers: courseSelectors.getAllTeachers(store),
    tags: courseSelectors.getAllTags(store),
    
    // 课程数据操作
    fetchCourses: store.fetchCourses,
    addCourse: store.addCourse,
    updateCourse: store.updateCourse,
    deleteCourse: store.deleteCourse,
    batchDeleteCourses: store.batchDeleteCourses,
    searchCourses: store.searchCourses,
    
    // 分类数据操作
    fetchCategories: store.fetchCategories,
    fetchCategoryTree: store.fetchCategoryTree,
    addCategory: store.addCategory,
    updateCategory: store.updateCategory,
    deleteCategory: store.deleteCategory,
    initializePresetCategories: store.initializePresetCategories,
    
    // 课程选择操作
    selectCourse: store.selectCourse,
    toggleCourseSelection: store.toggleCourseSelection,
    selectAllCourses: store.selectAllCourses,
    clearCourseSelection: store.clearCourseSelection,
    
    // 分类选择操作
    selectCategory: store.selectCategory,
    
    // 课程UI操作
    openCreateCourseForm: () => {
      store.setCourseFormMode('create');
      store.selectCourse(null);
      store.setShowCourseForm(true);
    },
    openEditCourseForm: (course: ICourse) => {
      store.selectCourse(course);
      store.setCourseFormMode('edit');
      store.setShowCourseForm(true);
    },
    closeCourseForm: () => {
      store.setShowCourseForm(false);
      store.selectCourse(null);
    },
    openCourseDetail: (course: ICourse) => {
      store.selectCourse(course);
      store.setShowCourseDetail(true);
    },
    closeCourseDetail: () => {
      store.setShowCourseDetail(false);
    },
    openCourseBatchPanel: () => {
      store.setShowCourseBatchPanel(true);
    },
    closeCourseBatchPanel: () => {
      store.setShowCourseBatchPanel(false);
      store.clearCourseSelection();
    },
    
    // 分类UI操作
    openCreateCategoryForm: () => {
      store.setCategoryFormMode('create');
      store.selectCategory(null);
      store.setShowCategoryForm(true);
    },
    openEditCategoryForm: (category: ICourseCategory) => {
      store.selectCategory(category);
      store.setCategoryFormMode('edit');
      store.setShowCategoryForm(true);
    },
    closeCategoryForm: () => {
      store.setShowCategoryForm(false);
      store.selectCategory(null);
    },
    openCategoryPanel: () => {
      store.setShowCategoryPanel(true);
    },
    closeCategoryPanel: () => {
      store.setShowCategoryPanel(false);
    },
    
    // 筛选操作
    updateCourseFilters: store.updateCourseFilters,
    setCourseSearchKeyword: store.setCourseSearchKeyword,
    setCourseSorting: store.setCourseSorting,
    resetCourseFilters: store.resetCourseFilters,
    updateCategoryFilters: store.updateCategoryFilters,
    setCategorySearchKeyword: store.setCategorySearchKeyword,
    resetCategoryFilters: store.resetCategoryFilters,
    
    // 工具函数
    getCourseById: (id: string) => courseSelectors.getCourseById(store, id),
    getCourseByCourseCode: (courseCode: string) => courseSelectors.getCourseByCourseCode(store, courseCode),
    getCategoryById: (id: string) => courseSelectors.getCategoryById(store, id),
    isCourseSelected: (courseId: string) => courseSelectors.isCourseSelected(store, courseId),
    
    // 重置操作
    reset: store.reset,
    refresh: store.refresh
  };
};

/**
 * 课程表单 Hook
 * @description 专门用于课程表单操作的Hook
 */
export const useCourseForm = () => {
  const store = useCourseStore();

  return {
    // 表单状态
    showForm: store.showCourseForm,
    formMode: store.courseFormMode,
    selectedCourse: store.selectedCourse,
    isLoading: courseSelectors.isCourseLoading(store),
    error: courseSelectors.getCourseError(store),
    
    // 分类数据（用于表单选择）
    categories: courseSelectors.getActiveCategories(store),
    categoryTree: courseSelectors.getCategoryTree(store),
    
    // 表单操作
    openCreateForm: () => {
      store.setCourseFormMode('create');
      store.selectCourse(null);
      store.setShowCourseForm(true);
    },
    openEditForm: (course: ICourse) => {
      store.selectCourse(course);
      store.setCourseFormMode('edit');
      store.setShowCourseForm(true);
    },
    closeForm: () => {
      store.setShowCourseForm(false);
      store.selectCourse(null);
    },
    
    // 数据操作
    addCourse: async (courseData: ICourseCreateRequest) => {
      await store.addCourse(courseData);
    },
    updateCourse: async (id: string, courseData: Omit<ICourseUpdateRequest, 'id'>) => {
      await store.updateCourse(id, courseData);
    },
    
    // 工具函数
    getCourseByCourseCode: (courseCode: string) => courseSelectors.getCourseByCourseCode(store, courseCode),
    getCategoryById: (id: string) => courseSelectors.getCategoryById(store, id)
  };
};

/**
 * 课程分类管理 Hook
 * @description 专门用于课程分类管理的Hook
 */
export const useCategoryManagement = () => {
  const store = useCourseStore();

  // 初始化分类数据
  useEffect(() => {
    if (store.categories.length === 0 && !store.categoryAsync.loading) {
      store.fetchCategories();
      store.fetchCategoryTree();
    }
  }, [store.categories.length, store.categoryAsync.loading, store.fetchCategories, store.fetchCategoryTree]);

  return {
    // 数据状态
    categories: courseSelectors.getFilteredCategories(store),
    allCategories: courseSelectors.getAllCategories(store),
    selectedCategory: store.selectedCategory,
    categoryTree: courseSelectors.getCategoryTree(store),
    presetCategories: courseSelectors.getPresetCategories(store),
    customCategories: courseSelectors.getCustomCategories(store),
    
    // 异步状态
    isLoading: courseSelectors.isCategoryLoading(store),
    error: courseSelectors.getCategoryError(store),
    lastUpdated: courseSelectors.getCategoryLastUpdated(store),
    
    // UI状态
    showForm: store.showCategoryForm,
    formMode: store.categoryFormMode,
    showPanel: store.showCategoryPanel,
    
    // 筛选状态
    filters: store.categoryFilters,
    
    // 统计信息
    statistics: courseSelectors.getCategoryStatistics(store),
    
    // 数据操作
    fetchCategories: store.fetchCategories,
    fetchCategoryTree: store.fetchCategoryTree,
    addCategory: store.addCategory,
    updateCategory: store.updateCategory,
    deleteCategory: store.deleteCategory,
    initializePresetCategories: store.initializePresetCategories,
    
    // 选择操作
    selectCategory: store.selectCategory,
    
    // UI操作
    openCreateForm: () => {
      store.setCategoryFormMode('create');
      store.selectCategory(null);
      store.setShowCategoryForm(true);
    },
    openEditForm: (category: ICourseCategory) => {
      store.selectCategory(category);
      store.setCategoryFormMode('edit');
      store.setShowCategoryForm(true);
    },
    closeForm: () => {
      store.setShowCategoryForm(false);
      store.selectCategory(null);
    },
    openPanel: () => {
      store.setShowCategoryPanel(true);
    },
    closePanel: () => {
      store.setShowCategoryPanel(false);
    },
    
    // 筛选操作
    updateFilters: store.updateCategoryFilters,
    setSearchKeyword: store.setCategorySearchKeyword,
    resetFilters: store.resetCategoryFilters,
    
    // 工具函数
    getCategoryById: (id: string) => courseSelectors.getCategoryById(store, id),
    getCategoriesByParent: (parentId?: string) => courseSelectors.getCategoriesByParent(store, parentId),
    getCategoriesByLevel: (level: number) => courseSelectors.getCategoriesByLevel(store, level)
  };
};

/**
 * 课程筛选 Hook
 * @description 专门用于课程筛选功能的Hook
 */
export const useCourseFilter = () => {
  const store = useCourseStore();

  return {
    // 筛选状态
    filters: store.courseFilters,
    filteredCourses: courseSelectors.getFilteredCourses(store),
    
    // 筛选选项
    categories: courseSelectors.getActiveCategories(store),
    teachers: courseSelectors.getAllTeachers(store),
    tags: courseSelectors.getAllTags(store),
    
    // 筛选操作
    updateFilters: store.updateCourseFilters,
    setSearchKeyword: store.setCourseSearchKeyword,
    setSorting: store.setCourseSorting,
    resetFilters: store.resetCourseFilters,
    
    // 搜索操作
    searchCourses: store.searchCourses,
    
    // 工具函数
    getCoursesByCategory: (categoryId: string) => courseSelectors.getCoursesByCategory(store, categoryId),
    getCoursesByTeacher: (teacher: string) => courseSelectors.getCoursesByTeacher(store, teacher),
    getCoursesByLevel: (level: 'beginner' | 'intermediate' | 'advanced') => courseSelectors.getCoursesByLevel(store, level),
    getCoursesByTag: (tag: string) => courseSelectors.getCoursesByTag(store, tag)
  };
};

/**
 * 课程选择 Hook
 * @description 专门用于课程选择功能的Hook
 */
export const useCourseSelection = () => {
  const store = useCourseStore();

  return {
    // 选择状态
    selectedCourseIds: store.selectedCourseIds,
    selectedCourses: courseSelectors.getSelectedCourses(store),
    selectedCount: courseSelectors.getSelectedCourseCount(store),
    hasSelected: courseSelectors.hasSelectedCourses(store),
    isAllSelected: courseSelectors.isAllCoursesSelected(store),
    isIndeterminate: courseSelectors.isCoursesIndeterminate(store),
    
    // 选择操作
    toggleSelection: store.toggleCourseSelection,
    selectAll: store.selectAllCourses,
    clearSelection: store.clearCourseSelection,
    isCourseSelected: (courseId: string) => courseSelectors.isCourseSelected(store, courseId)
  };
};

/**
 * 课程统计 Hook
 * @description 专门用于课程统计信息的Hook
 */
export const useCourseStatistics = () => {
  const store = useCourseStore();

  return {
    // 课程统计数据
    courseStatistics: courseSelectors.getCourseStatistics(store),
    activeCourses: courseSelectors.getActiveCourses(store),
    suspendedCourses: courseSelectors.getSuspendedCourses(store),
    
    // 分类统计数据
    categoryStatistics: courseSelectors.getCategoryStatistics(store),
    activeCategories: courseSelectors.getActiveCategories(store),
    presetCategories: courseSelectors.getPresetCategories(store),
    customCategories: courseSelectors.getCustomCategories(store),
    
    // 分组数据
    teachers: courseSelectors.getAllTeachers(store),
    tags: courseSelectors.getAllTags(store),
    
    // 工具函数
    getCoursesByCategory: (categoryId: string) => courseSelectors.getCoursesByCategory(store, categoryId),
    getCoursesByTeacher: (teacher: string) => courseSelectors.getCoursesByTeacher(store, teacher),
    getCoursesByLevel: (level: 'beginner' | 'intermediate' | 'advanced') => courseSelectors.getCoursesByLevel(store, level),
    getCategoriesByLevel: (level: number) => courseSelectors.getCategoriesByLevel(store, level)
  };
};